package com.zqwh.xtool.model.online;

import android.content.Context;

import com.zqwh.yj.sdk.utils.httpRequest.RetrofitSingleton;
import com.zqwh.yj.sdk.utils.httpRequest.subscribers.NetCallback;
import com.zqwh.yj.sdk.utils.httpRequest.subscribers.ProgressSubscriber;
import com.zqwh.yj.sdk.utils.httpRequest.HttpResultFunc2;
import com.zqwh.xtool.api.online.ToolApi;
import com.zqwh.xtool.config.BaseConfig;
import com.zqwh.xtool.domain.CategoryVO;
import com.zqwh.xtool.domain.ToolVO;
import com.zqwh.xtool.model.ToolModel;

import java.util.List;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

import static com.zqwh.xtool.ToolsApp.gUser;

public class ToolModelOnlineImpl implements ToolModel {
    private ToolApi toolApi;
    private Context context;

    public ToolModelOnlineImpl(Context context) {
        this.context = context;
        this.toolApi = RetrofitSingleton.getInstance(BaseConfig.BASE_URL).create(ToolApi.class);
    }

    @Override
    public void epcs2tools(String epcs, NetCallback<List<ToolVO>> callback) {
        toolApi.epcs2tools("Bearer "+gUser.getToken(), epcs)
                .map(new HttpResultFunc2<List<ToolVO>>())
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ProgressSubscriber(callback, context, false));
    }

    @Override
    public void getToolsBoundToDevice(Long devId, Long categoryId, Boolean hasRFID, NetCallback<List<ToolVO>> callback) {
        toolApi.getToolsBoundToDevice("Bearer "+gUser.getToken(), devId, categoryId, hasRFID)
                .map(new HttpResultFunc2<List<ToolVO>>())
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ProgressSubscriber(callback, context, false));
    }

    @Override
    public void bindTags(Long toolId, String tags, boolean cover, NetCallback<Object> callback) {
        toolApi.bindTags("Bearer "+gUser.getToken(), toolId, tags, cover)
                .map(new HttpResultFunc2<>())
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ProgressSubscriber(callback, context, false));
    }

    @Override
    public void getCategoryList(Long devId, NetCallback<List<CategoryVO>> callback) {
        toolApi.getCategoryList("Bearer "+gUser.getToken(), devId)
                .map(new HttpResultFunc2<List<CategoryVO>>())
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ProgressSubscriber(callback, context, false));
    }
}
